package com.longkey.workflow.client.canvas
{
	import flash.display.Graphics;
	import mx.graphics.SolidColorStroke;
	
	import spark.primitives.Line;
	
	public class ArrowLine extends Line
	{
		[Bindable]
		public var arrowWidth:Number = 6;
		
		[Bindable]
		public var bidirection:Boolean = false;
		
//		[Bindable]
//		public var color:Number = 0x0;
//		
//		[Bindable]
//		public var weight:Number = 1;
		
		public function ArrowLine()
		{
			super();
//			var stroke:SolidColorStroke = new SolidColorStroke();
//			stroke.color = this.color;
//			stroke.weight = this.weight;
//			this.stroke = stroke;
			
		}
		
		override protected function beginDraw(g:Graphics):void
		{
			g.beginFill(0x0000ff);
			super.beginDraw(g);
		}
		
		override protected function draw(g:Graphics):void
		{
			super.draw(g);
			this.drawArrow(g, this.xFrom, this.yFrom, this.xTo, this.yTo);
			if(this.bidirection){
				this.drawArrow(g, this.xTo, this.yTo, this.xFrom, this.yFrom);
			}
		}
		
		private function drawArrow(g:Graphics, xf:Number, yf:Number, xt:Number, yt:Number):void{
			var xDist:Number = xt - xf;
			var yDist:Number = yt- yf;
			
			var arrRotation:Number = Math.atan(yDist/xDist);
			
			if(xDist < 0) arrRotation += Math.PI;
			
			var radRotation:Number = 30*Math.PI / 180;
			
			var rotationUp:Number = arrRotation + radRotation;
			var rotationDown:Number = arrRotation - radRotation;
			
			var x1:Number = Math.round(xt - Math.cos(rotationUp) * arrowWidth);
			var y1:Number = Math.round(yt - Math.sin(rotationUp) * arrowWidth);
			
			var x2:Number = Math.round(xt - Math.cos(rotationDown) * arrowWidth);
			var y2:Number = Math.round(yt - Math.sin(rotationDown) * arrowWidth);
			

			
			g.moveTo(xt, yt);
			g.lineTo(x1,y1);
			g.lineTo(x2,y2);
			g.lineTo(xt, yt);
		}
	}
}